import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.*;

public class ReadHtml {

    private final Document document;

    public ReadHtml(Document document) {
        this.document = document;
    }

    //    获取总个数
    public int getAll() {
        Elements all = document.getAllElements();
        return all.size();
    }

    // 获取所有标签以及对应出现次数
    public Map<String, Integer> findAll() {
        Elements all = document.select("*");
       Elements all1 = document.getAllElements();
        all.forEach(System.out::println);
        System.out.println(">>>>>>>>>>>>>>>>>");
        all1.forEach(System.out::println);
        System.out.println(">>>>>>>>>>>>>>>>>");
        System.out.println(all.equals(all1));
        Map<String, Integer> map = new HashMap<>();
        for (Element element : all) {
            if (!map.containsKey(element.tagName())) {
//                map.put(element.tagName(), document.select(element.tagName()).size());
//            }
                map.put(element.tagName(),1);
            } else
                map.put(element.tagName(), map.get(element.tagName()) +1);
        }
        return map;
    }

    //4：用java解析bdqn.html,分析每个标签出现的次数
    // 然后输出,类似
    //  div 13次
    //  li  20次
    //存入Map,放入新元素的判断是否已存在，不存在，加入，存在，数量加1
    public static void main(String[] args) throws Exception {
         Document document = Jsoup.parse(new File("C:/Users/admin/Desktop/bdqn.html"), "utf-8");
      //  Document document = Jsoup.connect("https://www.jd.com/?cu=true&utm_source=baidu-pinzhuan&utm_medium=cpc&utm_campaign=t_288551095_baidupinzhuan&utm_term=0f3d30c8dba7459bb52f2eb5eba8ac7d_0_d199c8cb43f34349865a03721ace2339").get();
        ReadHtml rr = new ReadHtml(document);
        System.out.println("共有标签" + rr.getAll() + "个");
        System.out.println("---------------------------------------");
        Map<String, Integer> map = rr.findAll();
        Set<String> set = map.keySet();
        for (String s : set) {
            System.out.println(s + "标签有:" + map.get(s) + "个\n");
        }
        System.out.println("---------------------------------------");
        System.out.println("一共有:" + map.size() + "个标签");
        System.out.println(Arrays.toString(map.values().toArray()));
        System.out.println(Arrays.toString(map.keySet().toArray()));
    }
}


